package com.hd.appcommon.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.entity.DdoaCourseReplace;
import com.hd.appcommon.entity.DdoaCourseReplaceCourse;
import com.hd.appcommon.mapper.DdoaCourseReplaceMapper;
import com.hd.appcommon.service.DdoaBpmService;
import com.hd.appcommon.service.DdoaCourseReplaceCourseService;
import com.hd.appcommon.service.DdoaCourseReplaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (DdoaCourseReplace)表服务实现类
 *
 * @author makejava
 * @since 2023-07-06 19:53:47
 */
@Service
public class DdoaCourseReplaceServiceImpl extends ServiceImpl<DdoaCourseReplaceMapper, DdoaCourseReplace> implements DdoaCourseReplaceService {

    @Autowired
    DdoaBpmService ddoaBpmService;
    @Autowired
    DdoaCourseReplaceService ddoaCourseReplaceService;
    @Autowired
    DdoaCourseReplaceCourseService ddoaCourseReplaceCourseService;

    //更新学分替代审批
    public void update(String processInstancesId) throws Exception {
        //只需要更新oa审批基础数据表中的部分数据即可
        JSONObject res = ddoaBpmService.update(processInstancesId);
    }

    //将课程替代申请的审批数据保存到数据库
    public void save(String processInstancesId) throws Exception {

        //先在oa审批表中保存审批的基础数据
        JSONObject res = ddoaBpmService.save(processInstancesId);

        //获取课程替代审批的信息
        JSONArray form = res.getJSONArray("formComponentValues");

        //将审批的信息（List格式）转换为map格式
        Map<String, String> formMap = new HashMap<>();
        for (int i = 0; i < form.size(); i++) {
            JSONObject item = form.getJSONObject(i);
            formMap.put(item.getString("name"), item.getString("value"));
        }

        //根据map生成DdoaCourseReplace实体类
        DdoaCourseReplace courseReplace = new DdoaCourseReplace();
        courseReplace.setMajor(formMap.get("专业"));
        courseReplace.setName(formMap.get("姓名"));
        courseReplace.setReason(formMap.get("申请替代原因"));
        courseReplace.setSno(formMap.get("学号"));
        courseReplace.setColleges(formMap.get("开课学院"));
        courseReplace.setEvidences(formMap.get("相关证明材料"));
        courseReplace.setProcessInstanceId(processInstancesId);


        //下面需要将，课程替代表单中的，已修课程和拟替代课程信息保存
        List<DdoaCourseReplaceCourse> courseList = new ArrayList<>();

        //先转换已修课程的数据
        JSONArray courses1 = JSON.parseArray(formMap.get("已修课程"));
        for (int i = 0; i < courses1.size(); i++) {
            JSONObject item = courses1.getJSONObject(i);
            JSONArray course = item.getJSONArray("rowValue");
            DdoaCourseReplaceCourse crc = getCRC(course);
            crc.setProcessInstanceId(processInstancesId);
            crc.setReplaceType(0);
            courseList.add(crc);
        }

        JSONArray courses2 = JSON.parseArray(formMap.get("拟代替课程"));
        for (int i = 0; i < courses2.size(); i++) {
            JSONObject item = courses2.getJSONObject(i);
            JSONArray course = item.getJSONArray("rowValue");
            DdoaCourseReplaceCourse crc = getCRC(course);
            crc.setProcessInstanceId(processInstancesId);
            crc.setReplaceType(1);
            courseList.add(crc);
        }

        ddoaCourseReplaceService.save(courseReplace);
        ddoaCourseReplaceCourseService.saveBatch(courseList);
    }

    //将已修课程和拟替代课程的List格式信息转换为DdoaCourseReplaceCourse实体类
    public DdoaCourseReplaceCourse getCRC(JSONArray course) {
        Map<String, String> formMap = new HashMap<>();
        for (int i = 0; i < course.size(); i++) {
            JSONObject item = course.getJSONObject(i);
            formMap.put(item.getString("label"), item.getString("value"));
        }

        DdoaCourseReplaceCourse crc = new DdoaCourseReplaceCourse();
        crc.setCourName(formMap.get("课程名称"));
        crc.setCourCode(formMap.get("课程编码"));
        crc.setCourCollege(formMap.get("课程名称"));
        crc.setCourType(formMap.get("课程性质"));
        crc.setCourCredit(Float.valueOf(formMap.get("课程学分")));
        crc.setCourScores(formMap.get("成绩"));

        return crc;
    }

    public Map<String, Object> json2map(JSONObject req) {

        Map<String, Object> data = new HashMap<>();

        data.put("姓名", req.getString("name"));
        data.put("开课学院", req.getString("college"));
//        data.put("相关证明材料","测试材料");
        data.put("学号", req.getString("sno"));
        data.put("专业", req.getString("major"));
        data.put("申请替代原因", req.getString("reason"));

        JSONArray replaceCourseList = req.getJSONArray("replaceCourseList");


        List<Map> replaceCourseMapList = new ArrayList<>();
        for (int i = 0; i < replaceCourseList.size(); i++) {
            JSONObject item = replaceCourseList.getJSONObject(i);
            Map<String, Object> course = new HashMap<>();
            course.put("课程名称", item.getString("courName"));
            course.put("课程性质", item.getString("courAttr"));
            course.put("课程编码", item.getString("courCode"));
            course.put("课程学分", item.getString("courCredit"));
            replaceCourseMapList.add(course);
        }

        JSONArray finishCourseList = req.getJSONArray("finishCourseList");
        List<Map> finishCourseMapList = new ArrayList<>();
        for (int i = 0; i < finishCourseList.size(); i++) {
            JSONObject item = finishCourseList.getJSONObject(i);
            Map<String, Object> course = new HashMap<>();
            course.put("课程名称", item.getString("courName"));
            course.put("课程性质", item.getString("courAttr"));
            course.put("课程编码", item.getString("courCode"));
            course.put("课程学分", item.getString("courCredit"));
            course.put("成绩", item.getString("courScore"));
            finishCourseMapList.add(course);
        }


        data.put("拟代替课程", replaceCourseMapList);

        data.put("已修课程", finishCourseMapList);

        System.out.println(data);

        return data;
    }

}
